package com.shanghaionstar.home.presenter;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.mag.enumtype.CategoryType;
import com.onstar.cn.mag.publicService.response.ResponBannerEntity;
import com.onstar.cn.more.response.PersonalInfoResponse;
import com.shanghaionstar.discover.service.DiscoverService;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.home.iview.IRenewalView;
import com.shanghaionstar.user.service.AccountInfoService;
import com.shanghaionstar.utils.BannerUtil;

import java.util.List;

import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func1;

/**
 * Created by 7828wc on 16/6/29.
 */
public class RenewalPresenter extends BasePresenter<Object> {
    private IRenewalView mRenewalView;
    private AccountInfoService accountInfoService;
    private DiscoverService mService;

    public RenewalPresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
    }

    @Override
    public void setView(IMVPView<Object> view) {
        super.setView(view);
        mRenewalView = (IRenewalView) view;
        accountInfoService = new AccountInfoService(view.onActivity());
        mService = new DiscoverService(view.onActivity());
        setServiceProvider(accountInfoService);
    }

    public void commandData() {
        BannerUtil util = new BannerUtil(mRenewalView.onActivity());
        subscription = util.commandDataList(CategoryType.RENEW_BANNER.value()).subscribeOn(mvpScheduler.backgroundThread()).observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<List<ResponBannerEntity>>() {
                    @Override
                    public void call(List<ResponBannerEntity> response) {
                        if (response != null && response.size() > 0) {
                            mRenewalView.onGetBanner(response);
                        } else {
                            mRenewalView.onGetBanner(null);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        if (throwable != null && mRenewalView != null)
                            mRenewalView.onGetBanner(null);
                    }
                });
    }

    public void onBannerClick(ResponBannerEntity entity){
        BannerUtil util =  new BannerUtil(mRenewalView.onActivity());
        util.bannerClick(entity,mRenewalView.onActivity(), FunctionNameFindId.UnSend,mvpScheduler,mRenewalView);
    }

    public void updateUserInfo(final String licenseExpireDate) {
        mRenewalView.setLoading(true);
        subscription = accountInfoService.init().flatMap(new Func1<String, Observable<PersonalInfoResponse>>() {
            @Override
            public Observable<PersonalInfoResponse> call(String entity) {
                return accountInfoService.updateUserInfo(licenseExpireDate);
            }
        }).subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread()).subscribe(new Action1<PersonalInfoResponse>() {
                    @Override
                    public void call(PersonalInfoResponse geofenceList) {
                        mRenewalView.setLoading(false);
                        mRenewalView.onReponseSuccess(geofenceList);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //抛出Error异常
                        mRenewalView.onResponseError((OnStarException) throwable);
                        mRenewalView.setLoading(false);
                    }
                });
    }




}
